using System;
using System.Data;
using System.Data.SqlClient;
using Framework.Library;

namespace Framework.Module.UpDownLoad.Library
{
    public class Comments_Controller
    {
        public static DataTable GetAll()
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Share_Comments_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
		public static void Delete (int _iD)
		{
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("Share_Comments_Delete", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ID", _iD);
			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
			}
			finally
			{
				dbConn.Close();
			}
		}
        public static int Insert(Comments_Info _share_CommentInfo)
		{
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("Share_Comments_Insert", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Author", _share_CommentInfo.Author);
            dbCmd.Parameters.AddWithValue("@Comment", _share_CommentInfo.Comment);
            dbCmd.Parameters.AddWithValue("@DateCreate", _share_CommentInfo.DateCreate);
            dbCmd.Parameters.AddWithValue("@FilesID", _share_CommentInfo.FilesID);
            dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
			try {
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return (int)dbCmd.Parameters["@RETURN_VALUE"].Value;
			}
			finally 
			{
				dbConn.Close();
			}
		}

        public static void Update(Comments_Info _share_CommentInfo)
		{
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("Share_Comments_Update", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ID", _share_CommentInfo.ID);
            dbCmd.Parameters.AddWithValue("@Author", _share_CommentInfo.Author);
            dbCmd.Parameters.AddWithValue("@Comment", _share_CommentInfo.Comment);
            dbCmd.Parameters.AddWithValue("@DateCreate", _share_CommentInfo.DateCreate);
            dbCmd.Parameters.AddWithValue("@FilesID", _share_CommentInfo.FilesID);
			try {
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
			}
			finally
			{
				dbConn.Close();
			}
		}

        public static Comments_Info GetInfo(int _iD)
		{
            Comments_Info retVal = null;
 			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("Share_Comments_GetInfo", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ID", _iD);
			try 
			{
				dbConn.Open();
				SqlDataReader dr = dbCmd.ExecuteReader();
				if (dr.Read()) 
				{
                    retVal = new Comments_Info();
					retVal.ID=Convert.ToInt32(dr["ID"]);
					retVal.Author=Convert.ToString(dr["Author"]);
					retVal.Comment=Convert.ToString(dr["Comment"]);
					retVal.DateCreate=Convert.ToDateTime(dr["DateCreate"]);
					retVal.FilesID=Convert.ToInt32(dr["FilesID"]);
				}
				if (dr != null)	dr.Close();
			}
			finally
			{
				dbConn.Close();
			}
			return retVal;
		}
    }
}
